package com.dayuanit.dy15.atm.servlet;

import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.service.CardService;
import com.dayuanit.dy15.atm.service.impl.CardServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * 处理URL请求的；处理用户发送的请求
 * 面向用户的
 */
public class CardServlet extends BaseServlet {
    private CardService cardService = new CardServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求参数和请求地址，以及返回的信息，都需要提前约定好，
        //也就是要出一份文档来说明接口如何访问，如何响应。
        //这个文档就是：接口文档。
        //接口文档规定了：请求地址，请求方式，请求参数，参数类型，响应结果，响应类型等信息。
//Request(GET //127.0.0.1:8088/card?action=queryCards&userId=1002)@25860af7
//Request(GET //127.0.0.1:8088/card?action=queryCards&userId=1002)@7e4202f5
// request:Request(GET //127.0.0.1:8088/card?action=queryCards&userId=1002)@25860af7
// request:Request(GET //127.0.0.1:8088/card?action=queryCards&userId=1002)@7e4202f5
// request:Request(GET //127.0.0.1:8088/card?action=queryCards&userId=1002)@25860af7
// request:Request(GET //127.0.0.1:8088/card?action=queryCards&userId=1002)@25860af7

        //request:org.apache.catalina.connector.RequestFacade@fec461e
        //request:org.apache.catalina.connector.RequestFacade@fec461e
        //request:org.apache.catalina.connector.RequestFacade@fec461e
        //request:org.apache.catalina.connector.RequestFacade@2266436
        //request:org.apache.catalina.connector.RequestFacade@fec461e
        //request:org.apache.catalina.connector.RequestFacade@fec461e
        System.out.println(">>>>>>>request:" + req);
        req.setAttribute("color", "yellow");
        System.out.println("card color=" + req.getAttribute("color"));

        String action = req.getParameter("action");
        try {
            if ("openaccount".equals(action)) {
                openaccount(req, resp);
            }

            if ("deposit".equals(action)) {
                deposit(req, resp);
            }

            if ("draw".equals(action)) {
                draw(req, resp);
            }

            if ("transfer".equals(action)) {
                transfer(req, resp);
            }

            if ("queryCards".equals(action)) {
                queryMyCard(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseByAjax(ResponseDTO.fail(e.getMessage()), resp);
        }
    }

    private void openaccount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String password = req.getParameter("password");
        String confirmPassword = req.getParameter("confirmPassword");

        User user = (User)req.getSession().getAttribute("loginUser");
        cardService.openaccount(user.getId(), password, confirmPassword);
        //响应json
        responseByAjax(ResponseDTO.sucess(), resp);
    }

    private void deposit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String cardId = req.getParameter("cardId");
        String password = req.getParameter("password");
        String amount = req.getParameter("amount");

        cardService.deposit(Long.valueOf(cardId), password, amount);
        responseByAjax(ResponseDTO.sucess(), resp);
    }

    private void draw(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String cardId = req.getParameter("cardId");
        String password = req.getParameter("password");
        String amount = req.getParameter("amount");

        cardService.withdraw(Long.valueOf(cardId), password, amount);
        responseByAjax(ResponseDTO.sucess(), resp);
    }

    private void transfer(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String cardId = req.getParameter("cardId");
        String cardNumIn = req.getParameter("cardNumIn");
        String password = req.getParameter("password");
        String amount = req.getParameter("amount");

        cardService.transfer(Long.valueOf(cardId), cardNumIn, password, amount);
        responseByAjax(ResponseDTO.sucess(), resp);
    }

    private void queryMyCard(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //页面不能传userId，会有安全问题

        Cookie[] cookies = req.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }

        //session的创建 通过request.getSession();
        HttpSession session = req.getSession();

        System.out.println("sessionID=" + session.getId());
        User user = (User)session.getAttribute("loginUser");
        System.out.println(">>>>> card user=" + user);

        responseByAjax(ResponseDTO.sucess(cardService.queryCards(user.getId())), resp);
    }
}
